package com.qinghe.pro.util.log.impl;

import java.text.MessageFormat;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.qinghe.pro.entity.main.LogInfo;
import com.qinghe.pro.service.main.LogInfoService;
import com.qinghe.pro.shiro.ShiroUser;
import com.qinghe.pro.util.log.LogAPI;
import com.qinghe.utils.SecurityUtils;

public class LogAPIImpl implements LogAPI {

	private LogInfoService logInfoService;

	@Override
	public void log(String type, String message, Object[] objects) {
		MessageFormat mFormat = new MessageFormat(message);
		String result = mFormat.format(objects);

		if (StringUtils.isBlank(result)) {
			return;
		}

		ShiroUser shiroUser = SecurityUtils.getShiroUser();

		LogInfo logInfo = new LogInfo();
		logInfo.setCreateTime(new Date());
		logInfo.setUsername(shiroUser.getLoginName());
		logInfo.setType(type);
		logInfo.setMessage(result);
		logInfo.setIpAddress(shiroUser.getIpAddress());

		logInfoService.insert(logInfo);
	}

	public void setLogInfoService(LogInfoService logInfoService) {
		this.logInfoService = logInfoService;
	}

}
